<?php
/**
 * 文章中心
 * power by huanran.zhao
 * QQ: 523994553
 * 2013-04-09
 */
class GoodsApp extends CommonApp {
	
	private $table = 'goods';

	/**
	 * 首页
	 */
	public function indexAct(){
		//search
		$search = array();
		$search['where'] = "1 ";
		// $search['where'] .= "AND A.`item`='{$item}' ";
		//publish
		if(My::arr2var($_GET,'publish')==='0'){
			$search['where'] .= "AND publish=0 ";
		}elseif(My::arr2var($_GET,'publish')==1){
			$search['where'] .= "AND publish=1 ";
		}
		//搜索id
		if(My::is_id(My::arr2var($_GET,'id'))){
			$id = $_GET['id'];
			$search['where'] .= "AND `id`='{$id}' ";
		}
		if(My::arr2var($_GET,'title')!=''){
			$title = $_GET['title'];
			$search['where'] .= "AND `title` like '%{$title}%' ";
		}
		if(My::is_id(My::arr2var($_GET,'goods_type'))){
			$goods_type = $_GET['goods_type'];
			$search['where'] .= "AND `goods_type`='{$goods_type}' ";
		}
		if(isset($_GET['order'])) {
			$order = $_GET['order'];
			//排序字段为指派，正向排序为：当前用户最前，关闭最后，然后按照id正序排列
			$search['order'] = ($order=='assign') ? "`assign`={$userid} DESC, `assign`=0 ASC,`assign` DESC,`id` DESC" : $search['order'] = "`{$order}` DESC";
			//设置的字段排序与当前cookie排序一样的时候反向排序
			if(isset($_COOKIE['order']) && $search['order']==$_COOKIE['order']){
				//排序字段为指派，反向排序为：关闭最前，当前用户最后，然后按照id反序排列
				$search['order'] = ($order=='assign') ? "`assign`=0 DESC, `assign`={$userid} ASC,`assign` ASC,`editdate` DESC" : "`{$order}` ASC";
			}
			//字段排序存储cookie值一年
			setcookie('order', $search['order'], time()+31536000,'/');
			$this->ajaxReturn(200,'排序成功!');
		} else $search['order'] = isset($_COOKIE['order']) ? $_COOKIE['order'] : '`id` DESC';
		if(isset($_COOKIE['order']))
			$this->assign('order',$_COOKIE['order']);
		$search['page'] = My::arr2var($_REQUEST,'page').(isset($_SESSION['pagesize'])?$_SESSION['pagesize']:'');//用于参与缓存标识
		$search['pagesize'] = isset($_COOKIE['pagesize']) ? $_COOKIE['pagesize'] : '';
		$result=My::getCache($search,array('pre'=>$this->table));
		if($result==''){
			//page
			$count = DB($this->table)->where($search['where'])->count();
			$result['page'] = My::page($count,array('pagesize'=>$search['pagesize']));
			//result
			$result['data'] = DB($this->table)->where($search['where'])->order($search['order'])->limit($result['page']['limits'])->select();
			//echo DB('')->getLastSql();
			if(is_array($result['data']))
			foreach($result['data'] as &$value){
				$value['params'] = My::decodeJson($value['params']);
			}
			$result['type'] = M('admin.category')->item2submenu('goods_type',array('level'=>'1'));
			My::setCache($search,$result,array('pre'=>$this->table));
		}
		$this->assign('result',$result);
		$this->display();
	}
	
	/**
	 * 页面显示(编辑/添加)
	 * @params $_GET['id']
	 */
	public function modifyAct(){
		//分类唯一名称
		$item = My::arr2var($_GET,'item');
		if(!My::is_item($item))
			$this->ajaxReturn(404,"分类错误!");
		//id
		$id = I('get.id',0);
		$result = array();
		if(My::is_id($id)){
			//当前id文章数据
			$result = DB($this->table)->where("id={$id}")->find();
			$result['content'] = My::decodeHtml($result['content']);
			$result['params'] = My::decodeJson($result['params']);
			$this->assign('result',$result);
		}
		$goods_type = M('category')->item2level('goods_type',array('level'=>'1'));//商品类型
		$this->assign('goods_type',$goods_type);
		//分类id->自定义表单
		//$FORM = Form::item2FORM($item);
		//$this->assign('FORM',$FORM);
		$this->assign('item',$item);
		// $tpl = I('get.tpl',$item);
		$this->display();
	}
	
	/**
	 * 保存(编辑/添加)
	 * @param string $_POST['URL'] 当前页面url
	 * @param string $_POST['URL_'] 上一页面url
	 * @param int $_POST['id']  当前操作id(可选)
	 * @param $_POST post表单数据
	 */
	public function saveAct(){
		$item = My::arr2var($_POST,'item');
		if(!My::is_item($item))
			$this->ajaxReturn(300,'item有误!');
		$this->verify($item);
		if(!token())
			$this->ajaxReturn(300,'令牌验证错误!');
		if(My::arr2var($_POST,'title')=='')
			$this->ajaxReturn(300,"标题不能为空!");
		if(My::arr2var($_POST,'image')=='')
			$this->ajaxReturn(300,"图片不能为空!");
		if(My::arr2var($_POST,'goods_type')=='')
			$this->ajaxReturn(300,"请选择商品类别!");
		if(My::arr2var($_POST,'price')=='')
			$this->ajaxReturn(300,"请填写价格!");
		if(My::arr2var($_POST,'number')=='')
			$this->ajaxReturn(300,"请填写库存!");
		$params = My::arr2var($_POST,'params');
		if((!isset($params['image']) || end($params['image'])==''))
			$this->ajaxReturn(300,"商品轮番图片至少选择一张!");
		if(My::arr2var($_POST,'one_shop')=='1'){
			if(My::arr2var($_POST,'rate')=='')
				$this->ajaxReturn(300,"请填写中奖时间!");
			if(strtotime(My::arr2var($_POST,'rate')) <= strtotime(date("Y-m-d H:i:s")))
				$this->ajaxReturn(300,"中奖时间不能小于当前!");
			if(!isset($params['o_member']) || $params['o_member'] == '')
				$this->ajaxReturn(300,"参与人数不能小于等于0!");
		}
		if($params['resume'] == '')
			$this->ajaxReturn(300,"请填写相关商品简述!");
		if(My::arr2var($_POST,'content')=='')
			$this->ajaxReturn(300,"请填写商品图文详情!");
		$id = $_POST['id'] = My::arr2var($_POST,'id');
		if(My::is_id($id)){//编辑
			/*//白名单
			if($item=='activity')
				$params = My::keepParams($params,array('table'=>$this->table,'id'=>$id,'verifyId'=>'activity'));*/
			//保存表单模型参数
			$_POST['editdate'] = date("Y-m-d H:i:s");
			//dump($_POST);exit;
			$ok = DB($this->table)->clear()->save($_POST);
			if($ok)	$this->ajaxReturn(200,"编辑成功");
			else		$this->ajaxReturn(300,"编辑失败");
		}else{//添加
			$_POST['adddate'] = date("Y-m-d H:i:s");
			$_POST['author'] = My::user('username');
			$_POST['publish'] = '1';
			$ok = DB($this->table)->clear()->add($_POST);
			//保存表单模型参数
			// My::setParams($this->table,$params,$ok,$item);
			if($ok>=0)	$this->ajaxReturn(200,"添加成功");
			else		$this->ajaxReturn(300,"添加失败");
		}
	}

}